package com.whfc.common.face.wotu;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.whfc.common.util.HttpUtil;
import com.whfc.common.util.MD5Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;


/**
 * @author sun_guodong
 * @description
 * @date 2020-06-18
 */
public class WotuFaceApi {
    private static final Logger logger = LoggerFactory.getLogger(WotuFaceApi.class);

    private static final Integer SUCCESS_CODE = 1;

    /**
     * 服务器地址
     */
    private String server;

    /**
     * 应用appid
     */
    private String appId;

    /**
     * 应用key
     */
    private String appKey;

    /**
     * 应用secret
     */
    private String appSecret;

    public WotuFaceApi(String server, String appId, String appKey, String appSecret) {
        this.server = server;
        this.appId = appId;
        this.appKey = appKey;
        this.appSecret = appSecret;
    }

    /**
     * 鉴权获取token
     *
     * @return
     */
    public String auth() {
        String timestamp = String.valueOf(System.currentTimeMillis());
        String sign = sign(timestamp, appKey, appSecret);

        Map<String, String> param = new HashMap<>(4);
        param.put("appId", appId);
        param.put("appKey", appKey);
        param.put("timestamp", timestamp);
        param.put("sign", sign);
        String respond = HttpUtil.doPost(MessageFormat.format(WotuApiConst.SERVER + WotuApiConst.AUTH_URL, appId), param);
        logger.info("WotuFaceApi.auth,response:{}", respond);
        WotuApiResult<String> result = JSONObject.parseObject(respond, new TypeReference<WotuApiResult<String>>() {
        });
        if (SUCCESS_CODE.equals(result.getResult())) {
            return result.getData();
        }
        return null;
    }

    /**
     * 设备创建
     */
    public WotuApiResult<WotuApiAddDeviceResult> deviceCreate(String deviceKey, String name, String tag, String token) {
        logger.info("wotuApi添加设备:deviceKey:{},name:{},tag:{}", deviceKey, name, tag);

        Map<String, String> param = new HashMap<>(5);
        param.put("appId", appId);
        param.put("token", token);
        param.put("deviceKey", deviceKey);
        param.put("name", name);
        param.put("tag", tag);
        String respond = HttpUtil.doPost(MessageFormat.format(server + WotuApiConst.ADD_DEVICE_URL, appId), param);
        WotuApiResult<WotuApiAddDeviceResult> result = JSON.parseObject(respond, new TypeReference<WotuApiResult<WotuApiAddDeviceResult>>() {
        });

        logger.info("添加设备respond:{},entity:{}", respond, result.toString());
        return result;
    }

    /**
     * 删除设备
     *
     * @param deviceKey
     * @return
     */
    public WotuApiResult deviceDel(String deviceKey, String token) {
        logger.info("wotuApi删除设备:deviceKey:{}", deviceKey);

        Map<String, String> param = new HashMap<>();
        param.put("appId", appId);
        param.put("token", token);
        param.put("deviceKey", deviceKey);
        String respond = HttpUtil.doDelete(server, MessageFormat.format(WotuApiConst.DEL_DEVICE_URL, appId, deviceKey), param);
        WotuApiResult result = JSON.parseObject(respond, WotuApiResult.class);
        logger.info("删除设备respond:{},entity:{}", respond, result.toString());
        return result;
    }

    /**
     * 添加人员
     *
     * @param name
     * @param phone
     * @return
     */
    public WotuApiResult<WotuApiAddPersonResult> personCreate(String name, String phone, String token) {
        logger.info("wotuApi添加人员:name:{},phone:{}", name, phone);

        Map<String, String> param = new HashMap<>();
        param.put("appId", appId);
        param.put("token", token);
        param.put("name", name);
        param.put("phone", phone);
        String respond = HttpUtil.doPost(MessageFormat.format(server + WotuApiConst.ADD_PERSON_URL, appId), param);
        WotuApiResult<WotuApiAddPersonResult> result = JSON.parseObject(respond, new TypeReference<WotuApiResult<WotuApiAddPersonResult>>() {
        });
        logger.info("添加人员respond:{},entity:{}", respond, result.toString());
        return result;
    }

    /**
     * 添加人员照片
     *
     * @param personGuid
     * @param imageUrl
     * @return
     */
    public WotuApiResult<WotuApiAddFaceImgResult> faceImgCreate(String personGuid, String imageUrl, String token) {
        logger.info("wotuApi添加人员照片:personGuid:{},imageUrl:{}", personGuid, imageUrl);

        Map<String, String> param = new HashMap<>();
        param.put("appId", appId);
        param.put("token", token);
        param.put("guid", personGuid);
        param.put("imageUrl", imageUrl);
        String respond = HttpUtil.doPost(MessageFormat.format(server + WotuApiConst.ADD_PERSON_FACE_URL, appId, personGuid), param);
        WotuApiResult<WotuApiAddFaceImgResult> result = JSON.parseObject(respond, new TypeReference<WotuApiResult<WotuApiAddFaceImgResult>>() {
        });
        logger.info("添加人员照片respond:{},entity:{}", respond, result.toString());
        return result;
    }

    /**
     * 闸机授权人员
     *
     * @param deviceKey
     * @param personGuids
     * @return
     */
    public WotuApiResult setPeople(String deviceKey, String personGuids, String token) {
        logger.info("wotuApi闸机授权人员:deviceKey:{},personGuids:{}", deviceKey, personGuids);

        Map<String, String> param = new HashMap<>();
        param.put("appId", appId);
        param.put("token", token);
        param.put("deviceKey", deviceKey);
        param.put("personGuids", personGuids);
        String respond = HttpUtil.doPost(MessageFormat.format(server + WotuApiConst.ADD_SET_PEOPLE_URL, appId, deviceKey), param);
        WotuApiResult result = JSON.parseObject(respond, WotuApiResult.class);
        logger.info("闸机授权人员respond:{},entity:{}", respond, result.toString());
        return result;
    }

    /**
     * 闸机注销人员
     *
     * @param deviceKey
     * @param personGuids
     * @return
     */
    public WotuApiResult deleteSomePeople(String deviceKey, String personGuids, String token) {
        logger.info("wotuApi闸机注销人员:deviceKey:{},personGuids:{}", deviceKey, personGuids);

        Map<String, String> param = new HashMap<>();
        param.put("appId", appId);
        param.put("token", token);
        param.put("personGuids", personGuids);
        param.put("deviceKey", deviceKey);
        String respond = HttpUtil.doPost(MessageFormat.format(server + WotuApiConst.ADD_DELETE_PEOPLE_URL, appId, deviceKey), param);
        WotuApiResult result = JSON.parseObject(respond, WotuApiResult.class);
        logger.info("闸机注销人员respond:{},entity:{}", respond, result.toString());
        return result;
    }

    /**
     * 设备开门
     *
     * @param deviceKey
     * @param token
     * @return
     */
    public WotuApiResult deviceInteractiveRecord(String deviceKey, String token) {
        logger.info("wotuApi设备开门:deviceKey:{}", deviceKey);
        Map<String, String> param = new HashMap<>();
        param.put("appId", appId);
        param.put("token", token);
        param.put("deviceKey", deviceKey);
        param.put("type", "1");
        String respond = HttpUtil.doPost(MessageFormat.format(server + WotuApiConst.DEVICE_INTERACTIVE_RECORD, appId, deviceKey), param);
        WotuApiResult result = JSON.parseObject(respond, WotuApiResult.class);
        logger.info("设备开门respond:{},entity:{}", respond, result.toString());
        return result;
    }

    /**
     * 签名
     *
     * @param timestamp
     * @return
     */
    private static String sign(String timestamp, String appKey, String appSecret) {
        String md5 = MD5Util.md5Encode(new StringBuffer(appKey).append(timestamp).append(appSecret).toString());
        logger.info("md5:[{}]", md5);
        return md5;
    }
}
